package com.tanhua.server.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.tanhua.autoconfig.template.OssTemplate;
import com.tanhua.commons.utils.UserHolder;
import com.tanhua.dubbo.api.MovementApi;
import com.tanhua.dubbo.api.UserInfoApi;
import com.tanhua.dubbo.api.VisitorsApi;
import com.tanhua.model.domain.db.ErrorResult;
import com.tanhua.model.domain.db.User;
import com.tanhua.model.domain.db.UserInfo;
import com.tanhua.model.domain.mongodb.Movement;
import com.tanhua.model.domain.mongodb.Visitors;
import com.tanhua.model.vo.MovementsVo;
import com.tanhua.model.vo.PageResult;
import com.tanhua.model.vo.VisitorsVo;
import com.tanhua.server.exceotion.BusinessException;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @author maorf
 * @version 1.0
 */
@Service
public class MovementsService {
    @Autowired
    private OssTemplate ossTemplate;
    @DubboReference
    private MovementApi movementApi;
    @DubboReference
    private UserInfoApi userInfoApi;
    @DubboReference
    private VisitorsApi visitorsApi;

    public void publishMovement(Movement movement, MultipartFile[] imageContent) {
        //判断是否有数据
        if (ObjectUtils.isEmpty(movement.getTextContent())) {
            throw new BusinessException(ErrorResult.contentError());
        }
        //上传图片
        List<String> medias = new ArrayList<>();
        for (MultipartFile file : imageContent) {
            try {
                String url = ossTemplate.upload(file.getOriginalFilename(), file.getInputStream());
                medias.add(url);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        //调用远程接口保存数据
        movement.setUserId(UserHolder.getUserId());
        movement.setMedias(medias);
        movement.setState(3);
        movementApi.save(movement);

    }

    //查看好友动态
    public PageResult getMovements(Integer page, Integer pagesize) {
        PageResult pageResult = movementApi.findMovementsById(UserHolder.getUserId(), page, pagesize);
        List<Movement> items = (List<Movement>) pageResult.getItems();
        if (items == null) {
            return pageResult;
        }
        List<MovementsVo> list = new ArrayList<>();
        for (Movement item : items) {
            UserInfo userInfo = userInfoApi.findUserInfoById(item.getUserId());
            MovementsVo movementsVo = MovementsVo.init(userInfo, item);
            list.add(movementsVo);
        }
        pageResult.setItems(list);
        return pageResult;
    }

    //查看推荐动态
    public PageResult recommend(Integer page, Integer pagesize) {
        PageResult pageResult = movementApi.findRecommendMovementsById(UserHolder.getUserId(), page, pagesize);
        if (pageResult.getItems() == null) {
            return pageResult;
        }
        List<Movement> items = (List<Movement>) pageResult.getItems();
        List<MovementsVo> list = new ArrayList<>();
        for (Movement item : items) {
            UserInfo userInfo = userInfoApi.findUserInfoById(item.getUserId());
            MovementsVo movementsVo = MovementsVo.init(userInfo, item);
            list.add(movementsVo);
        }
        pageResult.setItems(list);
        return pageResult;
    }

    //查询个人动态
    public PageResult all(Long userId, Integer page, Integer pagesize) {
        PageResult pageResult = movementApi.FindOwnMovementsById(userId, page, pagesize);
        if (pageResult.getItems() == null) {
            return pageResult;
        }
        List<Movement> items = (List<Movement>) pageResult.getItems();
        List<MovementsVo> list = new ArrayList<>();
        for (Movement item : items) {
            UserInfo userInfo = userInfoApi.findUserInfoById(item.getUserId());
            MovementsVo movementsVo = MovementsVo.init(userInfo, item);
            list.add(movementsVo);
        }
        pageResult.setItems(list);
        return pageResult;
    }

    public MovementsVo findMovementById(String movementId) {
        Movement movements = movementApi.findById(movementId);
        if (movements == null) {
            return null;
        } else {
            UserInfo userInfo = userInfoApi.findUserInfoById(movements.getUserId());
            return MovementsVo.init(userInfo, movements);
        }
    }

    public List<VisitorsVo> queryVisitorsList() {
        User user = UserHolder.get();
        List<Visitors> visitorsList = visitorsApi.queryMyVisitor(user.getId());

        if (CollUtil.isEmpty(visitorsList)) {
            return Collections.emptyList();
        }
        List<Long> userIds = new ArrayList<>();
        for (Visitors visitors : visitorsList) {
            userIds.add(visitors.getVisitorUserId());
        }
        List<UserInfo> userInfoList = this.userInfoApi.findUserInfoByIds(userIds);

        List<VisitorsVo> visitorsVoList = new ArrayList<>();

        for (Visitors visitor : visitorsList) {
            for (UserInfo userInfo : userInfoList) {
                if (ObjectUtil.equals(visitor.getVisitorUserId(), userInfo.getId())) {
                    VisitorsVo visitorsVo = BeanUtil.toBeanIgnoreError(userInfo, VisitorsVo.class);
                    visitorsVo.setGender(userInfo.getGender());
                    visitorsVo.setFateValue(visitor.getScore().longValue());
                    visitorsVoList.add(visitorsVo);
                    break;
                }
            }
        }

        return visitorsVoList;
    }
}
